package org.dromara.web.job;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.unit.DataUnit;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ReUtil;
import cn.hutool.crypto.digest.DigestUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.aizuda.snailjob.client.job.core.annotation.JobExecutor;
import com.aizuda.snailjob.client.job.core.dto.JobArgs;
import com.aizuda.snailjob.client.model.ExecuteResult;
import com.aizuda.snailjob.common.core.util.JsonUtil;
import com.aizuda.snailjob.common.log.SnailJobLog;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.dromara.common.core.utils.SpringUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mail.utils.MailUtils;
import org.dromara.nft.domain.NftItem;
import org.dromara.nft.enums.NftType;
import org.dromara.nft.mapper.NftItemMapper;
import org.dromara.system.service.ISysConfigService;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * @author opensnail
 * @date 2024-05-17
 */
@Component
@JobExecutor(name = "ethJobExecutor")
@AllArgsConstructor
public class EthJobExecutor {


    public ExecuteResult jobExecute(JobArgs jobArgs) {

        String eth_yz = SpringUtils.getBean(ISysConfigService.class).selectConfigByKey("eth.yz");
        String[] split = jobArgs.getArgsStr().split(",");

        try {


            String s = HttpUtil.get("https://etherscan.io/token/0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2?a=0x1393ad734EA3c52865b4B541cf049dafd25c23a5");
            List<String> titles = ReUtil.findAll("<div id=\"ContentPlaceHolder1_divFilteredHolderBalance\" class=\"col-md border-md-start\">\n" +
                "<h6 class=\"small text-uppercase text-muted mb-1\">Balance</h6>\n" +
                "(.*?)" +
                "</div>", s, 1);

            if (CollUtil.isNotEmpty(titles)) {
                String t = titles.get(0);
                SnailJobLog.REMOTE.warn(eth_yz);
                SnailJobLog.REMOTE.warn(t);

                t = t.replace(" WETH\n", "");
                t = t.replace(",", "");

                if (NumberUtil.isLessOrEqual(new BigDecimal(t), new BigDecimal(eth_yz))) {
                    String mailText = "ETH:" + t + "小于" + eth_yz + "提醒<br> 时间" + DateUtil.format(new Date(), "yyyy年MM月dd日 HH时mm分ss秒");
                    SnailJobLog.REMOTE.warn(mailText);

                    MailUtils.sendHtml(split[0], "ETH提醒", mailText);
                }

                return ExecuteResult.success("获取成功");
            } else {
                throw new RuntimeException("获取失败");
            }

        } catch (Exception e) {
            SnailJobLog.REMOTE.warn("ETH异常提醒");
            MailUtils.sendHtml(split[0],"ETH异常提醒", "请检查服务！");
        }
        return ExecuteResult.failure("获取失败");
    }

    public static void main(String[] args) {

    }


}
